#include "WorkModeMenuText.h"

Factory < WorkModeMenuInterface > WorkModeMenuText::factory;

WorkModeMenuText::WorkModeMenuText ()
: selection ( 0 )
{
}

WorkModeMenuInterfacePtr WorkModeMenuText::Spawn ( WorkModeMenuTextLocalizationInterfacePtr argLocalization, TextStreamInterfacePtr argTextStream )
{
	WorkModeMenuTextPtr menu = factory.CreateNewObject < WorkModeMenuText > ();
	
	menu->localization = argLocalization;
	menu->textStream = argTextStream;
	
	return menu;
}

void WorkModeMenuText::CheckInnocence ()
{
	assert ( factory.ObjectsCount () == 0 );
}

void WorkModeMenuText::Run ( WorkModeReceiverInterfacePtr argReceiver )
{
	receiver = argReceiver;
	textStream->WriteString ( localization->Menu () );
	AcquireUserSelection ();
}

void WorkModeMenuText::AcquireUserSelection ()
{
	while ( true )
	{
		textStream->WriteString ( localization->InputWelcome () );
		ReadSelectionFromStdIn ();
		textStream->WriteString ( std::string ( 1, selection ) + "\n\r" );
		if ( SelectionMade () )
			break;
		else
			textStream->WriteString ( localization->InvalidSelection ( std::string ( 1, selection ) ) );
	}
}

void WorkModeMenuText::ReadSelectionFromStdIn ()
{
	selection = textStream->ReadChar ();
}

bool WorkModeMenuText::SelectionMade ()
{
	if ( selection == localization->SelectionCreateNew () )
		receiver->CreateNew ();
	else if ( selection == localization->SelectionOpenExisting () )
		receiver->OpenExisting ();
	else if ( selection == localization->SelectionChangeDesignation () )
		receiver->ChangeDesignation ();
	else if ( selection == localization->SelectionMakeCopy () )
		receiver->MakeCopy ();
	else if ( selection == localization->SelectionQuit () )
		receiver->Quit ();
	else if ( selection == localization->SelectionDeleteToTrashCan () )
		receiver->DeleteToTrashCan ();
	else if ( selection == localization->SelectionRestoreFromTrashCan () )
		receiver->RestoreFromTrashCan ();
	else if ( selection == localization->SelectionExtractFromPackage () )
		receiver->ExtractFromPackage ();
	else if ( selection == localization->SelectionImportFromFoxPro () )
		receiver->ImportFromFoxPro ();
	else if ( selection == localization->SelectionImportFromOracle () )
		receiver->ImportFromOracle ();
	else
		return false;
	return true;
}
